package org.zxy.auth.controller;

import org.zxy.auth.service.IRolePermissionService;
import org.zxy.auth.domain.RolePermission;
import org.zxy.auth.query.RolePermissionQuery;
import org.zxy.base.util.AjaxResult;
import org.zxy.base.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/rolePermission" )
public class RolePermissionController {
    @Autowired
    public IRolePermissionService rolePermissionService;


    /**
     * 保存和修改
     * @param rolePermission 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult insertOrUpdate(@RequestBody RolePermission rolePermission) {
        try {
            if ( rolePermission.getId() != null)
                rolePermissionService.update(rolePermission);
            else
                rolePermissionService.insert(rolePermission);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("保存对象失败！" + e.getMessage());
        }
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value = "/{id}" )
    public AjaxResult deleteById(@PathVariable("id" ) Long id) {
        try {
                rolePermissionService.deleteById(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("删除对象失败！" + e.getMessage());
        }
    }
    /**
     * 根据Id查询对象信息
     * @param
     * @return
     */
    //获取用户
    @GetMapping("/{id}" )
    public AjaxResult selectById(@PathVariable("id" ) Long id) {
        try {
            RolePermission rolePermission =rolePermissionService.selectById(id);
            return AjaxResult.success(rolePermission);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取一个失败！" + e.getMessage());
        }
    }


    /**
    * 查看所有对象信息
    * @return
    */
    @GetMapping
    public AjaxResult selectAll() {

        try {
            List< RolePermission> list = rolePermissionService.selectAll();
            return AjaxResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取所有失败！" + e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public AjaxResult queryPage(@RequestBody RolePermissionQuery query) {
        try {
            PageList<RolePermission> pageList = rolePermissionService.queryPage(query);
            return AjaxResult.success(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取分页数据失败！" + e.getMessage());
        }
    }

    /**
     * 批量删除对象
     *
     * @param
     * @return
     */
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        try {
                rolePermissionService.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("批量删除失败！" + e.getMessage());
        }
    }
}
